package cn.appsys.service.info;

import cn.appsys.dao.info.InfoMapper;
import cn.appsys.dao.version.VersionMapper;
import cn.appsys.pojo.Info;
import cn.appsys.pojo.Version;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.util.Date;
import java.util.List;

@Service
public class InfoServiceImpl implements InfoService {

    @Resource
    private InfoMapper infoMapper;
    @Resource
    private VersionMapper versionMapper;

    @Override
    public List<Info> findAppInfoList(String softwareName, Integer status,
                                      Integer flatformId,
                                      Integer categoryLevel1,
                                      Integer categoryLevel2,
                                      Integer categoryLevel3,
                                      Integer devId, Integer form,
                                      Integer pageSize) {
        return infoMapper.getAppInfoList(softwareName, status, flatformId,
                categoryLevel1, categoryLevel2, categoryLevel3, devId, form, pageSize);
    }

    @Override
    public int findAppInfoCount(String softwareName, Integer status, Integer flatformId, Integer categoryLevel1, Integer categoryLevel2, Integer categoryLevel3, Integer devId) {
        return infoMapper.getAppInfoCount(softwareName, status, flatformId,
                categoryLevel1, categoryLevel2, categoryLevel3, devId);
    }

    @Override
    public Info findAppInfo(Integer id, String APKName) {
        return infoMapper.getAppInfo(id,APKName);
    }

    @Override
    public boolean add(Info info) {
        return infoMapper.save(info)>0;
    }

    @Override
    public boolean delAppLog(Integer id) {
        return infoMapper.removeAppLog(id)>0;
    }

    @Override
    public boolean modify(Info info) {
        return infoMapper.update(info)>0;
    }

    @Override
    public boolean modifyVersionId(Integer versionId, Integer id) {
        return infoMapper.updateVersionId(versionId,id)>0;
    }

    @Override
    public boolean removeApp(Integer id) {
        boolean falg=false;
        int versionCount=versionMapper.versionCount(id);
        List<Version> versionList=null;
        if(versionCount>0){
            versionList=versionMapper.findVersionList(id);
            for(Version version:versionList){
                if(version.getApkLocPath()!=null && !version.getApkLocPath().equals("")){
                    File file=new File(version.getApkLocPath());
                    if(file.exists()){
                        if(!file.delete()){
                            try {
                                throw new Exception();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
            versionMapper.removeVersionByappId(id);
        }
        Info info=infoMapper.getAppInfo(id,null);
        if(info.getLogoLocPath()!=null&& !info.getLogoLocPath().equals("")){
            File file=new File(info.getLogoLocPath());
            if(file.exists()){
                if(!file.delete()){
                    try {
                        throw new Exception();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        if(infoMapper.delApp(id)>0){
            falg=true;
        }
        return  falg;

    }

    @Override
    public boolean appsysUpdateSaleStatusByAppId(Info info) {
        Info appinfo=infoMapper.getAppInfo(info.getId(),null);
        Integer operator=info.getModifyBy();
        if(operator<0 || info.getId()<0){
            try {
                throw new Exception();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if(null==appinfo){
            return false;
        }else{
            switch(appinfo.getStatus()){
                case 2:
                    onSale(appinfo,operator,4,2);
                    break;
                case 5:
                    onSale(appinfo,operator,4,2);
                    break;
                case 4:
                    offSale(appinfo,operator,5);
                    break;
                default:
                    return false;

            }
        }
        return true;
    }

    @Override
    public boolean updateStatus(Integer status, Integer id) {
        return infoMapper.upStatus(status,id)>0;
    }

    private void onSale(Info appinfo, Integer operator,
                        Integer appinfoStatus, Integer versionStatus) {
        offSale(appinfo,operator,appinfoStatus);
        setSaleSwitchToAppVersion(appinfo,operator,versionStatus);

    }

    private boolean offSale(Info appinfo, Integer operator, Integer appinfoStatus) {
        Info info=new Info();
        info.setModifyBy(operator);
        info.setId(appinfo.getId());
        info.setStatus(appinfoStatus);
        info.setOffSaleDate(new Date(System.currentTimeMillis()));
        infoMapper.modify(info);
        return true;
    }

    private boolean setSaleSwitchToAppVersion(Info appinfo, Integer operator, Integer versionStatus) {
        Version version=new Version();
        version.setId(appinfo.getVersionId());
        version.setPublishStatus(versionStatus);
        version.setModifyBy(operator);
        version.setModifyDate(new Date(System.currentTimeMillis()));
        versionMapper.modify(version);
        return false;
    }
}
